<template>
  <div class="userInfo-page">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>基本资料</span>
      </div>
      <!-- 修改信息 -->
      <el-form
        label-width="100px"
        class="demo-ruleForm"
        :model="form"
        :rules="rules"
        ref="formRef"
      >
        <el-form-item label="登录名称">
          <el-input :disabled="true" v-model="userInfo.username"></el-input>
        </el-form-item>
        <el-form-item label="用户名称" prop="nickname">
          <el-input v-model="form.nickname"></el-input>
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="revise">提交修改</el-button>
          <el-button @click="doReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { myUserinfoPut } from '@/api/user'
import { mapState } from 'vuex'
export default {
  name: 'userInfo-page',
  data () {
    return {
      form: {
        nickname: '',
        email: ''
      },
      rules: {
        nickname: [
          { required: true, message: '请输入用户名称', trigger: 'blur' }
        ],
        email: [{ required: true, message: '请输入邮箱', trigger: 'blur' }]
      }
    }
  },
  async created () {
    this.$store.dispatch('user/regUserInfo')
  },
  computed: {
    ...mapState('user', ['userInfo'])
  },
  methods: {
    doReset () {
      this.$refs.formRef.resetFields()
    },
    async revise () {
      this.$refs.formRef.validate()
      const res = await myUserinfoPut(this.userInfo)
      console.log(res)
      this.$store.commit('user/setUserInfo', {
        ...this.userInfo,
        nickname: this.nickname,
        email: this.email,
        username: this.userInfo.username
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.userInfo-page {
  padding: 80px 20px 0px 220px;

  .demo-ruleForm {
    width: 500px;
  }
}
</style>
